package com.songlin.utils;

import android.os.Environment;

import com.songlin.db.Person;
import com.songlin.db.State;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.File;
import java.util.List;

/**
 * Created by moqikaka on 17/5/26.
 */

//数据库单例类
public class DBUtils {

    //数据库管理类
    private DbManager mDbManager;

    //云端的状态码
    private int cloudState = 0;

    public int getCloudState() {
        return cloudState;
    }

    public void setCloudState(int cloudState) {
        this.cloudState = cloudState;
    }

    public DBUtils(){

        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                //设置数据库名，默认xutils.db
                .setDbName("bakejianshen322242.db")
                //设置是否允许事务，默认true
                //.setAllowTransaction(true)
                //设置数据库路径，默认安装程序路径下
                .setDbDir(new File(Environment.getExternalStorageDirectory().getAbsolutePath()))
                //设置数据库的版本号
                .setDbVersion(1)
                //设置数据库更新的监听
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {

                    }
                });

        mDbManager = x.getDb(daoConfig);

    };
    //私有的静态变量
    private static DBUtils dbUtils=null;
    //暴露的公有静态方法
    public static DBUtils getInstance() {
        if (dbUtils == null) {
            dbUtils = new DBUtils();
        }
        return dbUtils;
    }


    ////////////////会员表相关///////////////////////
    //添加一行数据
    public void saveOnePerson(Person person){

        try {
            mDbManager.save(person);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    //添加多行数据
    public void savePersons(List<Person> persons){

        try {
            mDbManager.save(persons);
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    public List<Person> getAllPersons() {

        List<Person> persons = null;
        try {
            persons = mDbManager.findAll(Person.class);
        } catch (DbException e) {
            e.printStackTrace();
        }

//        List<Person> persons = null;
//        try {
//            persons = mDbManager.selector(Person.class).orderBy("date").findAll();
//        } catch (DbException e) {
//            e.printStackTrace();
//        }

        return persons;
    }


    ////////////////状态码///////////////////////
    public void saveState(int num){

        try {
            State state = new State(num);
            mDbManager.save(state);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    public void upDateState(int num){

        State state = null;
        try {
            state = mDbManager.findFirst(State.class);
            state.setState(num);
            mDbManager.update(state,"state"); //c_name：表中的字段名
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    public int getState(){
        try {
            State firsts = mDbManager.findFirst(State.class);

            if(firsts == null){
                // 数据库没有就存入
                dbUtils.saveState(0);
                return 0;
            }else{
                return firsts.getState();
            }

        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public List<State> getAllState(){
        try {
            List<State> firsts = mDbManager.findAll(State.class); //  .findFirst(State.class);
            return firsts;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    //删除表中所有数据
    public void deleteAllDataInState(){
        try {
            mDbManager.delete(State.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

}
